package com.wcy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcy.annotation.aop.PreAuth;
import com.wcy.entity.Role;
import com.wcy.entity.queryVo.QueryConvertor;
import com.wcy.service.IRolePermissionService;
import com.wcy.service.IRoleService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private IRoleService roleService;
    @Resource
    private IRolePermissionService rolePermissionService;

    @PreAuth("ROLE:QUERY")
    @GetMapping("/page")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Page<Role>> getPage(@RequestParam(defaultValue = "1") int pageNum,
                                              @RequestParam(defaultValue = "10") int pageSize,
                                              HttpServletRequest request) {
        String queryData = request.getHeader("X-Query-Data");
        QueryWrapper wrapper = new QueryConvertor(queryData).convert(true);
        Page<Role> page = roleService.page(new Page<>(pageNum, pageSize),wrapper);
        return ResponseEntity.ok(page);
    }

    @PreAuth("ROLE:QUERY")
    @GetMapping("/list")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<List<Role>> getList(HttpServletRequest request) {
        String queryData = request.getHeader("X-Query-Data");
        QueryWrapper wrapper = new QueryConvertor(queryData).convert(true);
        List<Role> list = roleService.list(wrapper);
        return ResponseEntity.ok(list);
    }

    @PreAuth("ROLE:ADD")
    @Transactional
    @PostMapping("/add")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Void> add(@RequestBody Role role) {
        roleService.save(role);
        rolePermissionService.replaceRolePermission(role);
        return ResponseEntity.ok().build();
    }

    @PreAuth("ROLE:ALTER")
    @Transactional
    @PutMapping("/update")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Void> update(@RequestBody Role role) {
        roleService.updateById(role);
        rolePermissionService.replaceRolePermission(role);
        return ResponseEntity.ok().build();
    }

    @PreAuth("ROLE:DELETE")
    @DeleteMapping("/delete/{id}")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Void> delete(@PathVariable Integer id) {
        roleService.removeById(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/default")
    public ResponseEntity<List<Role>> getDefault(){
        return ResponseEntity.ok(roleService.list(new QueryWrapper<Role>().eq("is_default_role",true)));
    }
}